﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Sql;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.Editors;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using NHibernate.MappingGenerator.Properties;

namespace NHibernate.MappingGenerator
{
	public partial class ConnectionForm : Office2007Form
	{
		private IList<ServerConnectionParameters> serverList = null;
		public ConnectionForm()
		{
			InitializeComponent();
		}

		#region Utilities
		private void GetServers()
		{
			SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
			DataTable servers = instance.GetDataSources();

			foreach (DataRow server in servers.Rows)
			{
				DevComponents.Editors.ComboItem item = new ComboItem();

				ServerConnectionParameters parameters = new ServerConnectionParameters();

				parameters.ServerName = server["ServerName"].ToString() + (server["InstanceName"].ToString().Length > 0 ? @"\" + server["InstanceName"].ToString() : ""); ;

				parameters.ConnectionType = SqlConnectionTypeEnum.Windows;
				parameters.Username = "";
				parameters.Password = "";
				parameters.DisplayName = server["ServerName"].ToString() + (server["InstanceName"].ToString().Length > 0 ? @"\" + server["InstanceName"].ToString() : "")
						+ string.Format("({0})", server["Version"].ToString());

				item.Tag = parameters;
				item.Text = parameters.DisplayName;
				item.Image = Resources.icon_databaseserver;
				item.TextLineAlignment = StringAlignment.Center;

				serverList.Add(parameters);
				serversComboBox.Items.Add(item);
			}
		}

		private void ListServers()
		{
			foreach (ServerConnectionParameters server in serverList)
			{
				DevComponents.Editors.ComboItem item = new ComboItem();

				item.Tag = server;
				item.Text = server.DisplayName;
				item.Image = Resources.icon_databaseserver;
				item.TextLineAlignment = StringAlignment.Center;

				serversComboBox.Items.Add(item);
			}
		}
		#endregion

		private void ConnectionForm_Load(object sender, EventArgs e)
		{
			serverList = ServerConnectionParameters.GetSavedConnections();

			ListServers();
		}

		private void connectServerButton_Click(object sender, EventArgs e)
		{
			if (connectServerButton.Text.Equals("Connect"))
			{
				string serverName = string.Empty;

				if (serversComboBox.SelectedItem == null)
				{
					if (serversComboBox.Text.Length > 0)
						serverName = serversComboBox.Text;
				}
				else
				{
					ServerConnectionParameters parameters = (ServerConnectionParameters)((DevComponents.Editors.ComboItem)serversComboBox.SelectedItem).Tag; ;
					serverName = parameters.ServerName;
				}

				try
				{
					if (serverName.Length.Equals(0)) return;

					ServerConnection connection = new ServerConnection();

					connection.ServerInstance = serverName;

					connection.LoginSecure = windowsAuthCheckBox.Checked;
					if (!windowsAuthCheckBox.Checked)
					{
						connection.Login = usernameTextBox.Text;
						connection.Password = passwordTextBox.Text;
					}

					GeneratorContext.CurrentSqlServer = new Server(connection);

					MainForm form = new MainForm();
					form.ShowDialog(this);
				}
				catch (Exception exc)
				{
					throw;
				}
			}
		}

		private void refreshServersButton_Click(object sender, EventArgs e)
		{
			this.Cursor = Cursors.WaitCursor;
			GetServers();
			this.Cursor = Cursors.Default;
		}

		private void windowsAuthCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			usernameTextBox.Enabled = !windowsAuthCheckBox.Checked;
			passwordTextBox.Enabled = !windowsAuthCheckBox.Checked;
			usernameLabel.Enabled = !windowsAuthCheckBox.Checked;
			passwordLabel.Enabled = !windowsAuthCheckBox.Checked;
		}

		private void serversComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (serversComboBox.SelectedItem != null)
			{
				ServerConnectionParameters parameters = (ServerConnectionParameters)((DevComponents.Editors.ComboItem)serversComboBox.SelectedItem).Tag;

				windowsAuthCheckBox.Checked = parameters.ConnectionType.Equals(SqlConnectionTypeEnum.Windows);
				usernameTextBox.Enabled = !windowsAuthCheckBox.Checked;
				passwordTextBox.Enabled = !windowsAuthCheckBox.Checked;
				usernameLabel.Enabled = !windowsAuthCheckBox.Checked;
				passwordLabel.Enabled = !windowsAuthCheckBox.Checked;

				if (parameters.ConnectionType == SqlConnectionTypeEnum.Sql)
				{
					usernameTextBox.Text = parameters.Username;
					passwordTextBox.Text = parameters.Password;
				}
			}
		}
	}
}
